##############################
##############################
###System GMM Models Script###
##############################
##############################
library(lfe)
library(stargazer)
library(plm)
library(DataCombine)
library(dplyr)
library(survival)
library(MASS)
library(foreign)

####Subset data for relevant period
full.2013.2019 <- subset(full.1996.2019, (year>=2013&year<=2019))
set.seed(12321)
##############
###Baseline###
##############
subset.dat2 <- na.omit(full.2013.2019[,c("acled_battle_state","acled_battle_rebel","acled_battle_milall","acled_battle_polmil","acled_battle_idmil","log.nl","logged.pop","t","spei","p_anom","outbreak","t_anom","gid","month", "year")])  
#Convert data to pgmm format
gmm.dat.2 <- pdata.frame(subset.dat2, index=c("gid", "t"))

#####Armed conflict
##State
gmm.state.2 <- pgmm(formula=acled_battle_state ~ 
                      log.nl + logged.pop +
                      p_anom+spei+outbreak+t_anom| 
                      lag(acled_battle_state, 2:4),
                    data = gmm.dat.2, 
                    effect = "twoway", transformation= "ld", model = "onestep")
summary(gmm.state.2)
#Rebel
gmm.rebel.2 <- pgmm(formula=acled_battle_rebel ~ 
                      log.nl + logged.pop +
                      p_anom+spei+outbreak+t_anom| 
                      lag(acled_battle_rebel, 2:4),
                    data = gmm.dat.2, 
                    effect = "twoway", transformation= "ld", model = "onestep")
summary(gmm.rebel.2)

#####Social conflict
#Political militias
gmm.polmil.2 <- pgmm(formula=acled_battle_polmil ~ 
                       log.nl + logged.pop +
                       p_anom+spei+outbreak+t_anom| 
                       lag(acled_battle_polmil, 2:4),
                     data = gmm.dat.2, 
                     effect = "twoway", transformation= "ld", model = "onestep")
summary(gmm.polmil.2)
####Identify militias
gmm.idmil.2 <- pgmm(formula=acled_battle_idmil ~ 
                      log.nl + logged.pop +
                      p_anom+spei+outbreak+t_anom| 
                      lag(acled_battle_idmil, 2:4),
                    data = gmm.dat.2, 
                    effect = "twoway", transformation= "ld", model = "onestep")
summary(gmm.idmil.2)

#############
###Country###
#############
set.seed(12321)
##Keep only variables of interest
subset.dat3 <- na.omit(full.2013.2019[,c("acled_battle_state","acled_battle_rebel","acled_battle_polmil","acled_battle_idmil","log.nl","logged.pop","lagacled_battle_state","lagacled_battle_rebel","lagacled_battle_polmil","lagacled_battle_idmil","t","spei","p_anom","logLifeExpectancy","GovEff","logGDPpc","outbreak","t_anom","gid","month", "year")])  
#Convert data to pgmm format
gmm.dat.3 <- pdata.frame(subset.dat3, index=c("gid", "t"))

#####Armed conflict
##State
gmm.state.3 <- pgmm(formula=acled_battle_state ~ 
                      log.nl + logged.pop +
                      lagacled_battle_state + 
                      p_anom+spei+outbreak+t_anom+
                      logLifeExpectancy+GovEff+logGDPpc| 
                      lag(acled_battle_state, 2:4),
                    data = gmm.dat.3, 
                    effect = "twoway", transformation= "ld", model = "onestep")
summary(gmm.state.3)

#Rebel
gmm.rebel.3 <- pgmm(formula=acled_battle_rebel ~ 
                      log.nl + logged.pop +
                      lagacled_battle_rebel + 
                      p_anom+spei+outbreak+t_anom+
                      logLifeExpectancy+GovEff+logGDPpc| 
                      lag(acled_battle_rebel, 2:4),
                    data = gmm.dat.3, 
                    effect = "twoway", transformation= "ld", model = "onestep")
summary(gmm.rebel.3)

#####Social conflict
#Political militias
gmm.polmil.3 <- pgmm(formula=acled_battle_polmil ~ 
                       log.nl + logged.pop +
                       lagacled_battle_polmil + 
                       p_anom+spei+outbreak+t_anom+
                       logLifeExpectancy+GovEff+logGDPpc| 
                       lag(acled_battle_polmil, 2:4),
                     data = gmm.dat.3, 
                     effect = "twoway", transformation= "ld", model = "onestep")
summary(gmm.polmil.3)
####Identify militias 
gmm.idmil.3 <- pgmm(formula=acled_battle_idmil ~ 
                      log.nl + logged.pop +
                      lagacled_battle_idmil + 
                      p_anom+spei+outbreak+t_anom+
                      logLifeExpectancy+GovEff+logGDPpc| 
                      lag(acled_battle_idmil, 2:4),
                    data = gmm.dat.3, 
                    effect = "twoway", transformation= "ld", model = "onestep")
summary(gmm.idmil.3)
#Export to Latex
stargazer(gmm.state.2,gmm.state.3,gmm.rebel.2,gmm.rebel.3,gmm.polmil.2,gmm.polmil.3, gmm.idmil.2,gmm.idmil.3, ci = TRUE)


